﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/3/13 9:29:22
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Java;

namespace ImageK.Gui
{
	/** Implements the ROI Brush tool.*/
	public class RoiBrush
	{
	    static int ADD = 0, SUBTRACT = 1;
	    static int leftClick = 16, alt = 9, shift = 1;
	    private Polygon poly;
	    private Point previousP;
	    private int mode = ADD;

	    public RoiBrush()
	    {
		    Thread thread = new Thread(new ThreadStart(run));
            thread.Name = "RoiBrush";
            thread.Start();
	    }

	    public void run()
	    {
		    int size = ToolBar.getBrushSize();
		    ImagePlus img = WindowManager.getCurrentImage();
		    if (img == null) return;
		    ImageCanvas ic = img.getCanvas();
		    if (ic == null) return;
		    Roi roi = img.getRoi();
		    if (roi != null && !roi.isArea())
			    img.deleteRoi();
		    Point p = ic.getCursorLoc();
		    if (roi != null && !roi.contains(p.X, p.Y))
			    mode = SUBTRACT;
		    KeysMouseFlag flags;
		    while (true)
		    {
			    p = ic.getCursorLoc();
			    if (p.Equals(previousP))
			    { IJ.wait(1); continue; }
			    previousP = p;
			    flags = ic.getModifiers();
			    if ((flags.MouseButtons == MouseButtons.Left)) return;
			    if ((flags.ModifierKeys & Keys.Shift) == Keys.Shift)
				    mode = ADD;
			    else if ((flags.ModifierKeys & Keys.Alt) == Keys.Alt)
				    mode = SUBTRACT;
			    if (mode == ADD)
				    addCircle(img, p.X, p.Y, size);
			    else
				    subtractCircle(img, p.X, p.Y, size);
		    }
	    }

	    void addCircle(ImagePlus img, int x, int y, int width)
	    {
            throw new NotImplementedException();
			// Roi roi = img.getRoi();
		 //    Roi roi2 = roi;
		 //    if (roi2 != null)
		 //    {
			//     if (!(roi2 is ShapeRoi))
			// 	    roi2 = new ShapeRoi(roi2);
			//     ((ShapeRoi)roi2).or(getCircularRoi(x, y, width));
			//     roi2.copyAttributes(roi);
		 //    }
		 //    else
			//     roi2 = new OvalRoi(x - width / 2, y - width / 2, width, width);
		 //    img.setRoi(roi2);
	    }

	    void subtractCircle(ImagePlus img, int x, int y, int width)
	    {
            throw new NotImplementedException();
			// Roi roi = img.getRoi();
		 //    Roi roi2 = roi;
		 //    if (roi2 != null)
		 //    {
			//     if (!(roi2 is ShapeRoi))
			//     roi2 = new ShapeRoi(roi2);
			//     ((ShapeRoi)roi2).not(getCircularRoi(x, y, width));
			//     roi2.copyAttributes(roi);
			//     img.setRoi(roi2);
		 //    }
	    }


	    ShapeRoi getCircularRoi(int x, int y, int width)
        {
            throw new NotImplementedException();
            // if (poly == null)
            // {
            //  Roi roi = new OvalRoi(x - width / 2, y - width / 2, width, width);
            //  poly = roi.getPolygon();
            //  for (int i = 0; i < poly.npoints; i++)
            //  {
            //   poly.xpoints[i] -= x;
            //   poly.ypoints[i] -= y;
            //  }
            // }
            // return new ShapeRoi(x, y, poly);
        }
    }
}
